package com.deer.wms.intercept.annotation;

import cn.hutool.json.JSONUtil;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.MD5Utils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @Author: xu
 * @Date: 2022/08/08/9:04
 * @Description:
 */
@Slf4j
@Aspect
@Component
public class NoRepeatSubmitAop {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * api重复提交判断，没有考虑高并发情况
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.deer.wms.intercept.annotation.NoRepeatSubmit)")
    public Object around(ProceedingJoinPoint joinPoint)  {
        Object sendId = null;
        log.info("========api重复提交判断===========");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("access_token");
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取自定义注解
        NoRepeatSubmit annotation = methodSignature.getMethod().getAnnotation(NoRepeatSubmit.class);
        long timeout = annotation.timeout();
        //token+请求url为key
        BoundValueOperations valueOps = redisTemplate.boundValueOps(token + request.getRequestURI());
        if (valueOps.get() != null) {
            throw new ServiceException(CommonCode.AREA_UPPER_TACTIC_BOUND,"请勿重复提交，请稍后再试!");
        }
        valueOps.set(1, timeout, TimeUnit.SECONDS);
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            log.error("接口:{}调用异常，错误信息:{},参数信息：{}",request.getRequestURL(),throwable.getMessage(), JSONUtil.toJsonStr(request.getParameterMap()));
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, throwable.getMessage());
        }finally {
            redisTemplate.delete(token + request.getRequestURI());
            log.info("========api重复提交判断结束，删除key===========");
        }
        return proceed;
    }



//    @Around("@annotation(org.springframework.web.bind.annotation.GetMapping)")
//    public Object around01(ProceedingJoinPoint joinPoint)  {
//        Object sendId = null;
//        log.info("========api重复提交判断===========");
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String token = request.getHeader("access_token");
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        //获取自定义注解
////        NoRepeatSubmit annotation = methodSignature.getMethod().getAnnotation(NoRepeatSubmit.class);
//        long timeout = 10;
//        //token+请求url为key
//        BoundValueOperations valueOps = redisTemplate.boundValueOps(token + request.getRequestURI());
//        if (valueOps.get() != null) {
//            throw new ServiceException(CommonCode.AREA_UPPER_TACTIC_BOUND,"请勿重复提交，请稍后再试!");
//        }
//        valueOps.set(1, timeout, TimeUnit.SECONDS);
//        Object proceed = null;
//        try {
//            proceed = joinPoint.proceed();
//        } catch (Throwable throwable) {
//            log.error("接口:{}调用异常，错误信息:{},参数信息：{}",request.getRequestURL(),throwable.getMessage(), JSONUtil.toJsonStr(request.getParameterMap()));
//            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, throwable.getMessage());
//        }finally {
//            redisTemplate.delete(token + request.getRequestURI());
//            log.info("========api重复提交判断结束，删除key===========");
//        }
//        return proceed;
//    }


}
